<?php
// +----------------------------------------------------------------------
// | 首页控制器
// +----------------------------------------------------------------------

class index_controller extends base_controller {

	protected $tmp = array();

	public function _init(){
		$this->getLeftData();
		$this->getFooterData();
	}

	//首页
	public function index(){
		$files = dirname(APP_PATH).'/index.html';
		if(IS_CLI){
			$content = $this->_index();			
			file_put_contents($files, $content);
			echo $files."\n";
		}else{
			echo file_get_contents($files);
		}
	}

	public function _index(){
		$var = array();

		//首页顶部头条
		$arr = array(12=>'Celebrities', 10=>'Cars', 13=>'Movies');
		$tops = array();
		foreach($arr as $k=>$v){
			$rows = $this->image->getIndexTopList($k,true);
			$tmp = array();
			$tmp['name'] = $v;
			$tmp['url'] = url('category',$k);
			$tmp['lists'] = $this->image->format($rows,true);
			$tops[] = $tmp;
		}
		$var['tops'] = $tops;

		$field = 'id,name,catid1,catid2,img,view,download,addtime,updatetime';
		//最新壁纸
		$map = array();
		$rows = $this->image->getList($map, 8, $field, 'rand()', 'catid2', true);
		$var['list_news'] = $this->image->format($rows,true);

		//推荐壁纸
		$map = array();
		$rows = $this->image->getList($map, 8, $field, 'rand()', 'catid2', true);
		$var['list_recommend'] = $this->image->format($rows,true);

		//热门壁纸
		$map = array();
		$rows = $this->image->getList($map, 8, $field, 'rand()', 'catid2', true);
		$var['list_hots'] = $this->image->format($rows,true);

		//顶部推荐3条
		$map = array();
		$rows = $this->image->getList($map, 3, $field, 'rand()', 'catid2', true);
		$var['list_t3'] = $this->image->format($rows,true);

		//热门关键词
		$rows = $this->keywords->getHotKeywords(20);
		$var['hot_keywords'] = $this->keywords->format($rows);

		$this->assign($var);
		return $this->fetch("index_index");
	}

	//top news 排行页
	public function rank(){
		//$this->chkCmd();

		$time1 = time();
		$arr = array(
			'top'=>array('name'=>'Top Wallpapers', 'dir'=>'top-wallpapers', 'url'=>'/top-wallpapers/', 'order'=>'view desc'),
			'new'=>array('name'=>'New Wallpapers', 'dir'=>'new-wallpapers', 'url'=>'/new-wallpapers/', 'order'=>'addtime desc'),
		);
		C('rank_list',$arr);
		if($_GET['type'] && array_key_exists($_GET['type'], $arr)){
			$rows = array();
			$rows[$_GET['type']] = $arr[$_GET['type']];
			$arr = $rows;
		}

		$page = new page();
		foreach($arr as $k=>$v){
			echo "\n==================  ".date('Y-m-d H:i:s')." [ {$v['name']} ] create html!\n";
			$count = $this->image->where("status=1")->count();
			$count = 400;
			$page->init($count);

			for($p=1;$p<=$page->totalPages;$p++){
				$dir = '/'.$v['dir'].'/';
				$path = dirname(APP_PATH).$dir;
				$nowpage = $p==1 ? 'index.html' : "$p.html";
				$fileurl = $dir.$nowpage;
				$filepath = $path.$nowpage;

				//获取页面HTML
				$content = $this->_rank($k, $p);
				if(!is_dir($path)) mk_dir($path,0777);
				file_put_contents($filepath, $content);
				echo $fileurl."\n";
			}
		}

		$time2 = time()-$time1;
		echo "\n==================  run time: $time2 seconds  ==================\n";
	}

	protected function _rank($id,$page){
		//左边和底部公共数据
		$this->image->init();
		$this->getLeftData();
		$this->getFooterData();

		$var = array();
		$rank_list = C('rank_list');
		$var['info'] = $rank = $rank_list[$id];

		$field = 'id,name,catid1,catid2,img,view,download,addtime,updatetime';
		$order = $rank['order'];

		//图片列表
		C('sys_page_listrows', 20);
		$count = 400;

		load_class('page');
		$p = new page($count, $page);
		$conf = array('index_url' => "/{$rank['dir']}/", 'url' => "/{$rank['dir']}/".'{p}.html', 'setTheme' => 'en', 'theme'=>' %firstPage%  %prePage%  %linkPage%  %nextPage%  %lastPage%');
		foreach($conf as $k=>$v){
			$k=='setTheme' ? $p->setTheme($v) : $p->setConfig($k,$v);
		}
		$rows  = array();
		$rows['lists'] = $this->image->field($field)->where('status=1')->limit("{$p->firstRow},{$p->listRows}")->order($order)->findAll();
		$rows['pages'] = $p->show();
		$rows['pages_info'] = $p->show('array');
		$rows['lists'] = $this->image->format($rows['lists']);
		if($rows) $var = array_merge($var,$rows);

		//热门关键词
		if($this->tmp[$id]){
			$var['hot_keywords'] = $this->tmp['hot_keywords'];
		}else{
			$rows = $this->keywords->getHotKeywords(20);
			$this->tmp[$id] = $id;
			$this->tmp['hot_keywords'] = $var['hot_keywords'] = $this->keywords->format($rows);
		}

		$seo_page = $page>1 ? " page $page" : '';
		$this->assign('seo_title',$rank['name'].' Android & iPhone & iPad Wallpapers'.$seo_page);
		$this->assign('seo_keywords',$rank['name'].' Android & iPhone & iPad Wallpapers');
		$this->assign('seo_description','Free '.$rank['name'].' Android & iPhone & iPad wallpapers download, download free High Resolution, HD, Widescreen '.$rank['name'].' mobile wallpapers and pictures.');
		$this->assign('top_nav',$rank['dir']);

		$this->assign($var);
		$content = $this->fetch('index_rank');
		return $content;
	}

}